﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Warehouse.DataAccess
{
    public partial class MovingStock : CustomeEntity<MovingStock>
    {

        protected override System.Linq.Expressions.Expression<Func<MovingStock, bool>> GetPredicate()
        {
            return p => p.ID == this.ID;
        }

        #region Public Static Methods
        public static MovingStock GetStock(Guid Id)
        {
            return GetOne(p => p.ID == Id);
        }

        public static MovingItem[] Search(int fromHouseID, int toHouseID, DateTime startDate, DateTime endDate)
        {
            using (WarehouseClassesDataContext context = new WarehouseClassesDataContext())
            {
                return (from t in context.MovingStock
                        join h in context.Warehouse
                        on t.FromHouseID equals h.ID into th
                        let FromHouse = th.First().Name
                        join w in context.Warehouse
                        on t.ToHouseID equals w.ID into th1
                        let ToHouse = th1.First().Name
                        where (fromHouseID == 0 || t.FromHouseID == fromHouseID) && (toHouseID == 0 || t.ToHouseID == toHouseID) && t.MovingDate >= startDate && t.MovingDate < endDate
                        orderby t.MovingDate ascending
                        select new MovingItem
                        {
                            ID = t.ID,
                            FromHouse = FromHouse,
                            ToHouse = ToHouse,
                            MovingDate = t.MovingDate,
                            SerialNumber = t.SerialNumber,
                            Comments=t.Comments
                        }
                            ).ToArray();
            }
        }
        #endregion

        //static void CopyData(MovingStock source, MovingStock target)
        //{
        //    target.Comments = source.Comments;
        //    target.CreateDate = source.CreateDate;
        //    target.Creator = source.Creator;
        //    target.FromHouseID = source.FromHouseID;
        //    target.MovingDate = source.MovingDate;
        //    target.SerialNumber = source.SerialNumber;
        //    target.ToHouseID = source.ToHouseID;
        //}
    }
    public class MovingItem
    {
        public Guid ID { get; set; }
        public string FromHouse { get; set; }
        public string ToHouse { get; set; }
        public DateTime MovingDate { get; set; }
        public string SerialNumber { get; set; }
        public string Comments { get; set; }
    }
}
